$(function () {


    function query_host_or_goods_list() {
        var parm = {address_name: program_data.select_address2.name};

        parm.name = program_data.live_name;
        if (program_data.select_hot_search.id && program_data.select_hot_search.name == program_data.live_name) {
            parm.hot_search_id = program_data.select_hot_search.id;
        }
        if (program_data.select_address_goods_store2 && program_data.select_address_goods_store2.id) {
            parm.goods_store_id = program_data.select_address_goods_store2.id;
        }

        parm.is_only_address = program_data.select_address2_master;

        if (parm.is_only_address) {
            parm.store_address_id = program_data.select_address2.id;
        }
        parm.is_new_user = program_data.is_new_user;
        parm.live_type_id = program_data.home_fenlei_live_type_id;
        parm.page_index = 1;
        program_data.goods_parm = parm;
        program_data.goods_parm_page_index = 1;
        program_data.near_goods_list = [];
        api.sys.query_goods_by_phone_hot_search_list(parm).then(function (data) {
            program_data.near_goods_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                program_data.near_goods_list.push(item);
                if(item.comment.file_type==0){

                }else if(item.comment.file_type==1){
                    try{
                        item.comment.files_json = JSON.parse(item.comment.files_json);
                    }catch (e) {

                    }

                }else if(item.comment.file_type==2){
                    try {
                        item.comment.video_obj = JSON.parse(item.comment.videos_json);

                    }catch (e) {

                    }

                }
            });

        });

    };

    function query_store_address_list() {
        api.sys.query_store_address_list({is_active: true}).then(function (data) {
            if (data.success) {
                program_data.address_list = data.result.list;
                program_data.address_list2.push({name: "全部"});
                _(data.result.list).each(function (item) {
                    program_data.address_list2.push(item);
                })

                var name = client_storage("home_fenlei_select_address2");
                client_storage("home_fenlei_select_address2", "");
                _(program_data.address_list2).each(function (item) {
                    if(item.name==name){
                        program_data.select_address2 = item;
                    }
                })
            }

        });
    }

    function query_hot_search_list() {
        api.sys.query_hot_search_phone_list({is_active: true}).then(function (data) {
            if (data.success) {
                program_data.hot_search_list = data.result.list;

                var live_name = client_storage("home_fenlei_live_name");
                program_data.live_name = live_name;
                client_storage("home_fenlei_live_name", "");
                var is_new_user = client_storage("is_new_user");
                program_data.is_new_user = is_new_user;
                client_storage("is_new_user", "");
                var home_fenlei_live_type_id = client_storage("home_fenlei_live_type_id");
                program_data.home_fenlei_live_type_id = home_fenlei_live_type_id;
                client_storage("home_fenlei_live_type_id", "");
                _(program_data.hot_search_list).each(function (item) {
                    if(item.name==live_name){

                        program_data.select_hot_search = item;
                        program_data.select_address_goods_store2 = {};
                        // if(!program_data.select_address2.id){
                        //     program_data.select_address2 = {name: "全部"}
                        // }



                    }
                })

                query_host_or_goods_list();
            }

        });
    }

    var current_address = {name: "全球", class_name: "btn-danger", selected: true};


    function show_search() {
        program_data.show_search_content = true;
        program_data.show_live_list = false;

        if (program_data.select_address.id) {
            program_data.select_address2 = program_data.select_address;
        }

        program_data.select_address_goods_store2 = program_data.select_address_goods_store;

        // query_host_or_goods_list();
    }

    var program_data = {
        baseConfig: baseConfig,
        page_index: 0,
        page_count: 0,
        program_list: [],
        near_user_list: [],
        near_user_list_distance: [],
        query_user_list: [],
        near_goods_list: [],
        near_goods_count: 0,
        live_type_list: [],
        goods_type_list: [],
        goods_type_p_list: [],
        goods_type_s_list: [],
        address_list: [],
        address_list2: [],
        start_list:[{type:1, index:0},{type:1, index:1},{type:1, index:2},{type:1, index:3},{type:1, index:4}],
        address_goods_store_list: [],
        select_address_goods_store: {},
        select_address_goods_store2: {},
        select_address: {name: "选择地址"},
        select_hot_search: {},
        select_address2: {name: "全部"},
        select_address2_master: false,
        hot_search_list: [],
        select_goods_type: "全部",
        select_sub_goods_type: null,
        select_goods_type_id: null,
        has_searched: false,
        is_qita: false,
        is_new_user:false,
        goods_parm: {},
        goods_parm_page_index: 1,
        user_parm: {},
        user_parm_page_index: 1,
        live_name: "",
        search: "",
        address: "",
        show_search_content: false,
        show_search_content_half: true,
        search_content_half: 2,
        show_chat: false,
        show_host_info_obj: {},
        show_live_list: false,
        current_place: "",
        is_default: true,
        program_img: {},
        search_type: "goods",
        search_shi_ti: [],
        search_live_type: 2,
        hot_goods_type: [{name: "奶粉", class_name: "btn-default", selected: false, id: 13}, {
            name: "宝宝护理品",
            class_name: "btn-default",
            id: 15
        }, {name: '化妆品', class_name: "btn-default", id: 1}, {name: '鞋', class_name: "btn-default", id: 7}, {
            name: '包',
            class_name: "btn-default",
            id: 8
        }],// 奶粉，宝宝护理品，化妆品，鞋，包
        search_address: current_address,
        custom_search_address: "",
        search_address_list_1: [current_address, {name: "当前地图", class_name: "btn-default"}, {
            name: "潮汕",
            class_name: "btn-default"
        }],
        search_address_list_2: [{name: "香港", class_name: "btn-default"}, {
            name: "日本",
            class_name: "btn-default"
        }, {name: "韩国", class_name: "btn-default"}],
        live_map: {},

        user: {},
        show_talk: false,
        show_host_detail: false,
        show_img_talk: false,
        show_img_talk_more: false,
        show_pengyou_dongtai: false,
        show_pengyou_dongtai_images: false,
        all_pengyou_dongtai: [],
        show_contact: true,
        infoMap: {},
        recentSessMap: {},
        selToID: null,
        selKey: null,
        goodsstore_id: 0,
        contactlist: [],
        msglist: [],
        facelist: [],
        guanzhulist: [],
        guanzhudict: {},
        chat_user: null,
        chatcontent: "",
        live_status: false,
        program_id: null,

        live_config: {}
    };
    var tab_program = Vue.extend({
        template: "#tab_program",
        data: function () {
            return program_data;
        },
        methods: {
            show_head_img: show_head_img,
            price_filter: price_filter,
            open_pro_user: function (user_id) {
                location.href = './pro_user_goods.html?user_id=' + user_id;
            },
            go_friend_comment_list_by_comment: function (goods_id) {
                location.href = './goods_friend_comment_list.html?goods_id=' + goods_id;
            },
            open_pro_user_goods:function(gid, uid){
                location.href = 'goods.html?goods_id='+gid+'&pro_user_id='+uid;
            },
            get_fengmian:function(img_url1, img_url2){
                if(img_url1){
                    return baseConfig.qn_host + img_url1+'?imageView2/1/w/270/h/270';
                }else if(img_url2){
                    return baseConfig.qn_host + img_url2+'?imageView2/1/w/270/h/270';
                }
            },
            go_goods_detail: function (goods) {
                var phone_dict = phone_type();
                var phone = 'android';
                if (phone_dict.is_ios) {
                    phone = 'ios';
                }
                if (!goods.liveprogram_id) {
                    goods.liveprogram_id = "";
                }
                if (goods.goodsstore_id) {
                    location.href = 'play_' + phone + '.html?&show=goods&type=' + phone + '&program_id=' + goods.liveprogram_id + "&goodsstore_id=" + goods.goodsstore_id + "&t=" + new Date().getTime();
                } else {
                    location.href = 'play_' + phone + '.html?&show=goods&type=' + phone + '&program_id=' + goods.liveprogram_id + "&goodsstore_id=0&t=" + new Date().getTime();
                }
            },
            go_goods_info_detail: function (goods_id) {
                client_storage("home_fenlei_select_address2", program_data.select_address2.name);
                client_storage("home_fenlei_live_name", program_data.live_name);
                location.href = 'goods.html?goods_id=' + goods_id
            },
            show_search: show_search,
            select_address_item2: function (address, is_move) {
                program_data.select_address2 = address;
                program_data.select_address_goods_store2 = {};
                program_data.select_address2_master = false;
                if (program_data.live_name) {
                    query_host_or_goods_list();
                }
            },
            select_hot_search_fun: function (address) {
                program_data.live_name = address.name;
                program_data.select_hot_search = address;
                program_data.select_address_goods_store2 = {};
                program_data.select_address2 = {name: "全部"}
                query_host_or_goods_list();
            },

            change_search_live_type: function (live_type) {
                _(download_live).each(function (item, key) {
                    if (item['t'] == 1) {
                        return;
                    }
                    item.marker.setMap(null);
                    item.marker = null;
                    delete download_live[key];
                });
                program_data.search_live_type = live_type;
                show_host_by_location(map.center.lat(), map.center.lng(), get_map_zoom_level());
            },

            change_search_half: function (half_type) {

                program_data.search_content_half = half_type;
                if (half_type == 2) {
                    program_data.show_search_content_half = true;
                    sync_half_user();
                } else {
                    program_data.show_search_content_half = false;
                }


            },
            hide_pengyou_list: function () {
                program_data.show_pengyou_dongtai = false;
            },
            up_pengyou: function (p) {
                api.sys.zan_news({news_id: p.id}).then(function (result) {
                    if (result.result.is_zan) {
                        p.up_num += 1;
                    }

                })
            },
            open_pengyou: function (p) {
                program_data.show_pengyou_dongtai = false;
                create_chat({imusername: p.user__imusername, realname: p.user__realname});
            },
            show_pengyou: function () {
                program_data.show_pengyou_dongtai = true;
                var host_ids = [];
                _(program_data.near_user_list).each(function (item) {
                    host_ids.push(item.id)
                });
                api.sys.query_news_by_hosts_list({user_ids: host_ids.join(",")}).then(function (result) {
                    program_data.all_pengyou_dongtai = [];
                    _(result.result.list).each(function (item) {
                        if (item.files_json) {
                            item.files_json = JSON.parse(item.files_json);

                            // item.files_json_length = item.files_json.length;
                        }
                        item.goods_num = 0;
                        if (item.goods1_id) {
                            item.goods_num += 1;
                        }
                        if (item.goods2_id) {
                            item.goods_num += 1;
                        }
                        if (item.goods3_id) {
                            item.goods_num += 1;
                        }
                        if (item.goods4_id) {
                            item.goods_num += 1;
                        }
                        if (item.news_type == 2) {
                            item.video_obj = item.files_json[0];
                            item.video_pic = item.files_json[1];
                        }

                        program_data.all_pengyou_dongtai.push(item);
                    })
                });
                // $.getScript("data/pengyou_data.js", function() {
                //     program_data.all_pengyou_dongtai = [];
                //     _(pengyou_data_list).each(function (item) {
                //         if(item.files_json){
                //             item.files_json = JSON.parse(item.files_json);
                //
                //             // item.files_json_length = item.files_json.length;
                //         }
                //         item.goods_num = 0;
                //         if(item.goods1_id){
                //             item.goods_num += 1;
                //         }
                //         if(item.goods2_id){
                //             item.goods_num += 1;
                //         }
                //         if(item.goods3_id){
                //             item.goods_num += 1;
                //         }
                //         if(item.goods4_id){
                //             item.goods_num += 1;
                //         }
                //
                //         program_data.all_pengyou_dongtai.push(item);
                //     })
                //     // program_data.all_pengyou_dongtai = pengyou_data_list;
                // });
            },
            show_big_pengyou_img: function (item, fileurl) {
                // program_data.show_pengyou_dongtai = false;
                program_data.show_pengyou_dongtai_images = true;
                var width = window.innerWidth
                    || document.documentElement.clientWidth
                    || document.body.clientWidth;
                var height = window.innerHeight
                    || document.documentElement.clientHeight
                    || document.body.clientHeight;
                setTimeout(function () {
                    var pswpElement = document.getElementById("all_pengyou_pswp");

                    var items = [];
                    var index = 0;

                    _(item.files_json).each(function (sub, i) {
                        items.push({src: baseConfig.qn_host + sub.fileurl +'?imageView2/0/w/'+width+'/h/'+(width*4/3), w: width, h: (width*4/3)})
                        if (sub.fileurl == fileurl) {
                            index = i;
                        }
                    })

                    var options = {
                        // optionName: 'option value'
                        // for example:
                        mainClass: 'pswp--minimal--dark',
                        index: index, // start at first slide

                    };

                    var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
                    gallery.listen('close', function () {
                        // program_data.show_pengyou_dongtai = true;
                        program_data.show_pengyou_dongtai_images = false;
                    });
                    gallery.init();
                }, 200);

            },
            play_video: function (dt) {
                dt.clicked = true;
                var v = document.getElementById("video" + dt.id);
                if (!dt.playing) {
                    _(program_data.all_pengyou_dongtai).each(function (item) {
                        if (item.id != dt.id && item.playing) {
                            var v2 = document.getElementById("video" + item.id);
                            v2.pause();
                            item.playing = false;
                        }
                    })
                    v.play();
                    dt.playing = true;
                } else {
                    v.pause();
                    dt.playing = false;
                }
            },
            show_diff_time: function (dt) {
                var start_time = moment();
                var now = moment(dt);

                var days = start_time.diff(now, 'days');
                start_time = start_time.subtract(days, 'days');
                var hours = start_time.diff(now, 'hours');
                start_time = start_time.subtract(hours, 'hours');
                var minutes = start_time.diff(now, 'minutes');
                if (days == 0) {
                    if (hours > 0) {
                        return hours + "小时前";
                    }
                    return minutes + "分钟前"
                } else {
                    return dt.substr(5, 11);
                }
            },
            hide_search: function () {
                program_data.select_goods_type_id = null;
                program_data.select_sub_goods_type = null;
                program_data.select_goods_type = "全部";
                program_data.goods_type_s_list = [];
                program_data.live_name = "";
                // program_data.show_search_content = false;

            },
            hide_search_pannel: function () {
                program_data.show_search_content = false;

            },

            select_address_fun: function (obj) {
                _(program_data.search_address_list_1).each(function (item) {
                    item.class_name = "btn-default";
                    item.selected = false;
                });
                _(program_data.search_address_list_2).each(function (item) {
                    item.class_name = "btn-default";
                    item.selected = false;
                });
                obj.class_name = "btn-danger";
                obj.selected = false;
                program_data.search_address = obj;
                program_data.is_qita = false;
                // program_data.custom_search_address = "";
            },
            select_address_fun_qita: function () {
                _(program_data.search_address_list_1).each(function (item) {
                    item.class_name = "btn-default";
                    item.selected = false;
                });
                _(program_data.search_address_list_2).each(function (item) {
                    item.class_name = "btn-default";
                    item.selected = false;
                });
                program_data.search_address = null;
                program_data.is_qita = true;
                // program_data.custom_search_address = "";
            },
            change_custom_search_address: function (address_name) {
                if (address_name) {
                    _(program_data.search_address_list_1).each(function (item) {
                        item.class_name = "btn-default";
                        item.selected = false;
                    });
                    _(program_data.search_address_list_2).each(function (item) {
                        item.class_name = "btn-default";
                        item.selected = false;
                    });
                    program_data.search_address = null;
                } else {
                    if (program_data.search_address == null) {
                        program_data.search_address = current_address;
                        current_address.selected = true;
                        current_address.class_name = "btn-danger";
                    }

                }
            },

            query_host_or_goods_reset: function () {
                _(program_data.hot_goods_type).each(function (item) {
                    item.class_name = "btn-default";
                    item.selected = false;
                });
                _(program_data.search_address_list_1).each(function (item) {
                    item.class_name = "btn-default";
                    item.selected = false;
                });
                _(program_data.search_address_list_2).each(function (item) {
                    item.class_name = "btn-default";
                    item.selected = false;
                });
                program_data.search_address = current_address;
                program_data.is_qita = false;
                current_address.selected = true;
                current_address.class_name = "btn-danger";
                program_data.custom_search_address = "";
                program_data.live_name = "";
                program_data.search_shi_ti = [];
                program_data.has_searched = false;

            },

            query_host_or_goods_list: query_host_or_goods_list,

            query_search_goods_list: function () {
                var parm = program_data.goods_parm;
                if (program_data.near_goods_count == program_data.goods_parm_page_index) {
                    Magiclamp.mcAlert("提示", "已到最后一页。", null, function () {

                    });
                    return;
                }
                program_data.goods_parm_page_index += 1;
                parm.page_index = program_data.goods_parm_page_index;
                api.sys.query_goods_by_phone_hot_search_list(parm).then(function (data) {
                    program_data.near_goods_count = data.result.page_count;
                    _(data.result.list).each(function (item) {
                        program_data.near_goods_list.push(item);
                        if(item.comment.file_type==0){

                        }else if(item.comment.file_type==1){
                            try{
                                item.comment.files_json = JSON.parse(item.comment.files_json);
                            }catch (e) {

                            }

                        }else if(item.comment.file_type==2){
                            try {
                                item.comment.video_obj = JSON.parse(item.comment.videos_json);

                            }catch (e) {

                            }

                        }
                    });

                });
            },

            query_search_user_list: function () {
                var parm = program_data.user_parm;
                program_data.user_parm_page_index += 1;
                parm.page_index = program_data.user_parm_page_index;
                api.sys.query_host_by_gps_list(parm).then(function (data) {
                    _(data.result.list).each(function (item) {
                        program_data.near_user_list.push(item);
                    });

                });
            },

            select_goods_type_fun: function (obj) {
                if (obj.selected == true) {
                    obj.class_name = "btn-default";
                    obj.selected = false;
                } else {
                    _(program_data.hot_goods_type).each(function (item) {
                        item.class_name = "btn-default";
                        item.selected = false;
                    });
                    obj.class_name = "btn-danger";
                    obj.selected = true;
                }

            },
            select_add: function (add) {
                program_data.select_address = add;
            },
            select_live_list: function () {
                program_data.show_search_content = false;

                _(download_live).each(function (item, key) {
                    item.marker.setMap(null);
                    item.marker = null;
                    delete download_live[key];
                });
                show_host_by_location(map.center.lat(), map.center.lng(), get_map_zoom_level());

            },
            hide_live_list: function () {
                setTimeout(function () {
                    program_data.show_live_list = false;
                });
            },
            edit_address: function () {
                Magiclamp.mcPrompt2("请输入地区", program_data.address, function (data) {
                    if (!data) {
                        return;
                    }
                    program_data.address = data;
                    codeAddress(program_data.address).then(function (data) {

                    });
                })
            },
            edit_goods: function () {
                Magiclamp.mcPrompt2("请输入商品", program_data.live_name, function (data) {
                    if (!data) {
                        return;
                    }
                    program_data.live_name = data;
                    program_data.show_search_content = true;
                    program_data.show_live_list = false;
                    query_host_or_goods_list();

                })
            },
            change_add: function () {
                // alert("1");
                // 创建地址解析器实例
                codeAddress(program_data.address).then(function (data) {

                });

                // var myGeo = new BMap.Geocoder();
                // // 将地址解析结果显示在地图上，并调整地图视野
                // myGeo.getPoint(program_data.address, function(point){
                //         if (point) {
                //             map.centerAndZoom(point, 8);
                //             map.addOverlay(new BMap.Marker(point));
                //         }
                //     }, "日本国");
                // b_init(program_data.address).then(function () {});
            },
            show_host_info: function (obj) {
                program_data.show_host_detail = true;
                program_data.show_host_info_obj = obj;

                program_data.show_live_list = true;
                program_data.show_chat = false;
            },
            hide_host_info: function (obj) {
                program_data.show_host_detail = false;
                program_data.show_host_info_obj = obj;

                program_data.show_live_list = false;
                program_data.show_chat = false;
            },
            change_face_btn: function () {
                chat_data.show_img_talk = !chat_data.show_img_talk;
                chat_data.show_img_talk_more = false;
            },
            change_more_btn: function () {
                chat_data.show_img_talk_more = !chat_data.show_img_talk_more;
                chat_data.show_img_talk = false;
            },
            changeData: function (event) {
                this.chatcontent = event.srcElement.innerHTML;
            },
            select_goodstype: function (name, goods_type_id) {

                program_data.select_goods_type = name;
                var arr = [];
                if (goods_type_id != null) {
                    _(program_data.goods_type_list).each(function (item) {
                        if (item.parent_id == goods_type_id) {
                            arr.push(item);
                        }
                    });
                }

                program_data.goods_type_s_list = arr;
                if (arr.length > 0) {
                    program_data.select_goods_type_id = arr[0].id;
                    program_data.select_sub_goods_type = arr[0].name;
                } else {
                    program_data.select_goods_type_id = null;
                    program_data.select_sub_goods_type = null;
                }
            },

            select_sub_goodstype: function (name, goods_type_id) {
                program_data.select_goods_type_id = goods_type_id;
                program_data.select_sub_goods_type = name;
            },

            timeline_str: function () {
                var t = new Date().getTime();
                t = parseInt(t / 1000 / 60 / 3);
                return "?t=" + t;
            },
            open_program: function (program_id) {
                if (!program_id) {
                    program_id = "";
                }
                var phone_type_obj = phone_type();
                if (phone_type_obj.is_wx) {
                    if (phone_type_obj.is_ios) {
                        location.href = 'play_ios.html?type=ios&program_id=' + program_id + "&t=" + new Date().getTime();
                    } else if (phone_type_obj.is_android) {
                        location.href = 'play_android.html?type=android&program_id=' + program_id + "&t=" + new Date().getTime();
                    } else {
                        // 非android 和 ios 手机上的微信浏览器
                    }
                } else {
                    if (phone_type_obj.is_ios) {
                        location.href = 'play_ios.html?type=ios&program_id=' + program_id + "&t=" + new Date().getTime();
                    } else if (phone_type_obj.is_android) {
                        location.href = 'play_android.html?type=android&program_id=' + program_id + "&t=" + new Date().getTime();
                    } else {
                        // 非 ios 和 android的 系统的其他浏览器
                    }
                }

            },
            search_by_livetype: function (livetype_id) {
                _(program_data.live_type_list).each(function (item) {
                    item.checked = false;
                    if (item.id == livetype_id) {
                        item.checked = true;
                    }
                });
                program_data.program_list = [];
                program_data.page_index = 0;
                program_data.page_count = 0;
                program_data.livetype_id = livetype_id;
                var parm = {
                    page_index: program_data.page_index + 1,
                    name: program_data.search,
                    livetype_id: program_data.livetype_id
                }
                if (parm.livetype_id == 0) {
                    delete parm.livetype_id
                    parm['is_hot'] = true;
                }
                api.lprogram.query_live_program_list(parm).then(function (data) {

                    $.each(data.result.list, function (index, item) {
                        item.current_config__data_json = JSON.parse(item.current_config__data_json);
                        if (item.repeat != '') {
                            item.repeat = JSON.parse(item.repeat);
                            var time_time = '';
                            var time_week = '周';
                            $.each(item.repeat, function (index, time) {
                                time_time = time.startTime + '-' + time.endTime;
                                if (time.week == 1) {
                                    time_week += '一、';
                                } else if (time.week == 2) {
                                    time_week += '二、';
                                } else if (time.week == 3) {
                                    time_week += '三、';
                                } else if (time.week == 4) {
                                    time_week += '四、';
                                } else if (time.week == 5) {
                                    time_week += '五、';
                                } else if (time.week == 6) {
                                    time_week += '六、';
                                } else if (time.week == 7) {
                                    time_week += '日、';
                                }
                            });
                            item.time_repeat = time_time;
                            item.time_week = time_week.slice(0, -1)
                        }

                    });

                    _(data.result.list).each(function (item) {
                        program_data.program_list.push(item);
                    });

                    program_data.page_index = data.result.page_index;
                    program_data.page_count = data.result.page_count;

                });
            },
            search_by_name: function () {
                program_data.program_list = [];
                program_data.page_index = 0;
                program_data.page_count = 0;
                var parm = {
                    page_index: program_data.page_index + 1,
                    name: program_data.search,
                    livetype_id: program_data.livetype_id
                }
                if (parm.livetype_id == 0) {
                    delete parm.livetype_id
                    parm['is_hot'] = true;
                }
                api.lprogram.query_live_program_list(parm).then(function (data) {

                    $.each(data.result.list, function (index, item) {
                        item.current_config__data_json = JSON.parse(item.current_config__data_json);
                        if (item.repeat != '') {
                            item.repeat = JSON.parse(item.repeat);
                            var time_time = '';
                            var time_week = '周';
                            $.each(item.repeat, function (index, time) {
                                time_time = time.startTime + '-' + time.endTime;
                                if (time.week == 1) {
                                    time_week += '一、';
                                } else if (time.week == 2) {
                                    time_week += '二、';
                                } else if (time.week == 3) {
                                    time_week += '三、';
                                } else if (time.week == 4) {
                                    time_week += '四、';
                                } else if (time.week == 5) {
                                    time_week += '五、';
                                } else if (time.week == 6) {
                                    time_week += '六、';
                                } else if (time.week == 7) {
                                    time_week += '日、';
                                }
                            });
                            item.time_repeat = time_time;
                            item.time_week = time_week.slice(0, -1)
                        }

                    });

                    _(data.result.list).each(function (item) {
                        program_data.program_list.push(item);
                    });

                    program_data.page_index = data.result.page_index;
                    program_data.page_count = data.result.page_count;

                });
            },
            load_more_program: function () {
                var parm = {
                    page_index: program_data.page_index + 1,
                    name: program_data.search,
                    livetype_id: program_data.livetype_id
                }
                if (parm.livetype_id == 0) {
                    delete parm.livetype_id
                    parm['is_hot'] = true;
                }
                api.lprogram.query_live_program_list(parm).then(function (data) {
                    $.each(data.result.list, function (index, item) {
                        item.current_config__data_json = JSON.parse(item.current_config__data_json);
                        if (item.repeat != '') {
                            item.repeat = JSON.parse(item.repeat);
                            var time_time = '';
                            var time_week = '周';
                            $.each(item.repeat, function (index, time) {
                                time_time = time.startTime + '-' + time.endTime;
                                if (time.week == 1) {
                                    time_week += '一、';
                                } else if (time.week == 2) {
                                    time_week += '二、';
                                } else if (time.week == 3) {
                                    time_week += '三、';
                                } else if (time.week == 4) {
                                    time_week += '四、';
                                } else if (time.week == 5) {
                                    time_week += '五、';
                                } else if (time.week == 6) {
                                    time_week += '六、';
                                } else if (time.week == 7) {
                                    time_week += '日、';
                                }
                            });
                            item.time_repeat = time_time;
                            item.time_week = time_week.slice(0, -1)
                        }

                    });
                    _(data.result.list).each(function (item) {
                        program_data.program_list.push(item);
                    });

                    program_data.page_index = data.result.page_index;
                    program_data.page_count = data.result.page_count;

                });
            },
            shengyudays: function (plan_start_time, live_status) {
                if (live_status == 7) {
                    return "正在直播";
                } else if (!plan_start_time) {
                    return "等待直播";
                } else {
                    var start_time = moment(plan_start_time);
                    var now = moment();
                    if (start_time.isBefore(now)) {

                        return "等待直播";
                    }
                    var days = start_time.diff(now, 'days');
                    start_time = start_time.subtract(days, 'days');
                    var hours = start_time.diff(now, 'hours');
                    start_time = start_time.subtract(hours, 'hours');
                    var minutes = start_time.diff(now, 'minutes');
                    if (days == 0) {
                        return "(剩余：" + now.subtract(hours).hours() + "小时)"
                    } else if (days < 0) {
                        return "等待直播";
                    } else {
                        return "(剩余：" + days + "天" + hours + '小时' + minutes + '分)';
                    }
                }

            },
            has_contact_list: function (selToId) {
                var h = _(chat_data.contactlist).find(function (item) {
                    if (item.SessionId == selToId) {
                        return true;
                    }
                })
                if (!h) {
                    return false;
                } else {
                    return !h.is_hide;
                }
            },
            has_contact_or_guanzhu_list: function (selToId) {
                var is_contact = false;
                var h = _(chat_data.contactlist).find(function (item) {
                    if (item.SessionId == selToId) {
                        return true;
                    }
                });
                if (!h) {
                    is_contact = false;
                } else {
                    is_contact = !h.is_hide;
                }

                if (is_contact) {
                    return true;
                } else {
                    return !!chat_data.guanzhudict[selToId];
                }


            },
            clearLoop: function () {
                // window.ontouchstart = null;
                clearInterval(chat_data.Loop);//再次清空定时器，防止重复注册定时器
            },
            send_chat: function () {
                var text = $("#chat_ele").html();
                onSendMsg(text, function () {
                    chat_data.chatcontent = "";
                    $("#chat_ele").html("");
                })
            },
            add_face: function (face) {
                $("#chat_ele").append(face[0]);
                chat_data.chatcontent += face[0];
            },
            upload: function (face) {
                uploadPic(face);
            },
            chuoyichuo: function () {
                onSendMsg("戳一戳", function () {
                })
            },
            show_history: function () {
                getPrePageC2CHistoryMsgs();
            },
            price_filter: price_filter,
            show_head_img: show_head_img,
            showDeleteButton: function (contact) {
                clearInterval(chat_data.Loop);//再次清空定时器，防止重复注册定时器
                chat_data.Loop = setTimeout(function () {

                    var msg = "";
                    var imusername = "";
                    if (contact.SessionIdKey) {
                        msg = "是否删除" + chat_data.infoMap[contact.SessionIdKey].name + "的会话？";
                        imusername = contact.SessionId;
                    } else {
                        msg = "是否删除" + contact.realname + "的会话？";
                        imusername = contact.imusername;
                    }
                    Magiclamp.mcConfirm("提示", msg, function () {

                    }, function () {
                        delChat(contact.SessionType, contact.SessionId, function (resp) {
                            _(chat_data.contactlist).each(function (item) {
                                if (contact.SessionId == item.SessionId) {
                                    item.is_hide = true;
                                }
                            })
                        });
                        api.sys.remove_follow_by_imusername_list({host_imusername: imusername}).then(function (item) {
                            query_follow_by_user_list();
                        })
                    })

                }, 300);

            },
            go_goods_detail: function (goods) {
                var phone_dict = phone_type();
                var phone = 'android';
                if (phone_dict.is_ios) {
                    phone = 'ios';
                }
                if (!goods.liveprogram_id) {
                    goods.liveprogram_id = "";
                }
                // location.href = 'goods.html?goods_id='+goods_id;
                if (goods.goodsstore_id) {
                    location.href = 'play_' + phone + '.html?&show=goods&type=' + phone + '&program_id=' + goods.liveprogram_id + "&goodsstore_id=" + goods.goodsstore_id + "&t=" + new Date().getTime();
                } else {
                    location.href = 'play_' + phone + '.html?&show=goods&type=' + phone + '&program_id=' + goods.liveprogram_id + "&goodsstore_id=0&t=" + new Date().getTime();
                }

            },
        }
    });

    var create_chat = function (user) {
        open_chat(webim.SESSION_TYPE.C2C, user.imusername, user.realname, user.goodsstore_id);
        pushHistory();
    };

    var query_follow_by_user_list = function () {
        api.sys.query_follow_by_user_list().then(function (res) {
            window.chat_data.guanzhulist = res.result.list;
            window.chat_data.guanzhudict = {};
            _(res.result.list).each(function (item) {
                window.chat_data.guanzhudict[item.imusername] = true;
            })
        })
    };


    window.toggleTabs = function (tabText) {
        var defer = $.Deferred();
        if (tabText == "tab_program_fl") {
            // home_data.currentView = "tab_program";
            // client_storage("page", "tab_program");
            // home_data.active = "tab_program";
            // window.show_map();
            // show_search();
            // location.href = 'home_fenlei.html'
        } else {
            client_storage("page", tabText);
            location.href = 'home.html';
        }

        defer.resolve();
        return defer.promise();
    }

    window.home_data = {
        tab_programText: "tab_program",
        tab_programFlText: "tab_program_fl",
        tab_chatText: "tab_chat",
        tab_defaultText: "tab_default",
        tab_cartText: "tab_cart",
        tab_meText: "tab_me",
        currentView: "tab_program",
        chat_num: 0,
        all_unread: 0,
        is_default: true,
        active: 0
    };
    var home = new Vue({
        el: "#home",
        data: home_data,
        components: {
            tab_program: tab_program,
        },
        methods: {
            toggleTabs: toggleTabs,
            refresh_cart: function () {
                api.lgoods.query_cart_list().then(function (data) {
                    data.result.list.forEach(function (item) {
                        if (item.is_active) {
                            home_data.chat_num += item.num;
                        }
                    });
                });
            }

        }
    });

    $(function () {
        query_store_address_list();
        query_hot_search_list();
        home_data.active = "tab_program_fl";
        show_search();
        window.login_mogu_ask(true).then(function (value) {
            home.refresh_cart();
        });
    });

    window.login_mogu_ask = function (must) {
        var defer = $.Deferred();
        login_mogu(function () {
            defer.resolve();

        }, "home", null, must);
        return defer.promise();
    };


    home_data.sessionid = GetQueryString('sessionid');
    if (home_data.sessionid) {
        home_data.sessionid_q = "sessionid=" + home_data.sessionid;
        client_storage('sessionid', home_data.sessionid);
    }

});

document.setTitle = function (t) {
    document.title = t;
    var i = document.createElement('iframe');
    i.src = '//m.baidu.com/favicon.ico';
    i.style.display = 'none';
    i.onload = function () {
        setTimeout(function () {
            i.remove();
        }, 9)
    }
    document.body.appendChild(i);
}

setTimeout(function () {
    window.addEventListener("popstate", function (e) {
        if (home_data.active == "tab_chat") {
            if (chat_data.show_host_detail) {
                chat_data.show_host_detail = false;
            } else if (chat_data.show_talk) {
                pushHistory();
            } else if (chat_data.show_contact) {
                window.toggleTabs("tab_program");
            }
        } else if (home_data.active == "tab_program") {
            window.close();
        }
        // e.stop();
    }, false);
}, 300);

function pushHistory() {
    var state = {
        title: "title",
        url: "#"
    };
    window.history.pushState(state, "title", "#");
}
